home *** CD-ROM | disk | FTP | other *** search
/ TPUG - Toronto PET Users Group / TPUG Users Group CD / TPUG Users Group CD.iso / AMIGA / AMICUS / AMICUS02.ADF / Emacs / line.c < prev    next >
C/C++ Source or Header  |  1989-05-30  |  17KB  |  466 lines

  1. /* line.c */
  2.  
  3. /*
  4.  * The functions in this file are a general set of line management utilities.
  5.  * They are the only routines that touch the text. They also touch the buffer
  6.  * and window structures, to make sure that the necessary updating gets done.
  7.  * There are routines in this file that handle the kill buffer too. It isn't
  8.  * here for any good reason.
  9.  *
  10.  * Note that this code only updates the dot and mark values in the window list.
  11.  * Since all the code acts on the current window, the buffer that we are
  12.  * editing must be being displayed, which means that "b_nwnd" is non zero,
  13.  * which means that the dot and mark values in the buffer headers are nonsense.
  14.  */
  15.  
  16. #include        <stdio.h>
  17. #include        "ed.h"
  18.  
  19. #define NBLOCK  16                      /* Line block chunk size        */
  20. #define KBLOCK  256                     /* Kill buffer block size       */
  21.  
  22. char    *kbufp  = NULL;                 /* Kill buffer data             */
  23. int     kused   = 0;                    /* # of bytes used in KB        */
  24. int     ksize   = 0;                    /* # of bytes allocated in KB   */
  25.  
  26. /*
  27.  * This routine allocates a block of memory large enough to hold a LINE
  28.  * containing "used" characters. The block is always rounded up a bit. Return
  29.  * a pointer to the new block, or NULL if there isn't any memory left. Print a
  30.  * message in the message line if no space.
  31.  */
  32. LINE    *
  33. lalloc(used)
  34. register int    used;
  35. {
  36.         register LINE   *lp;
  37.         register int    size;
  38.  
  39.         size = (used+NBLOCK-1) & ~(NBLOCK-1);
  40.         if (size == 0)                          /* Assume that an empty */
  41.                 size = NBLOCK;                  /* line is for type-in. */
  42.         if ((lp = (LINE *) malloc(sizeof(LINE)+size)) == NULL) {
  43.                 mlwrite("Cannot allocate %d bytes", size);
  44.                 return (NULL);
  45.         }
  46.         lp->l_size = size;
  47.         lp->l_used = used;
  48.         return (lp);
  49. }
  50.  
  51. /*
  52.  * Delete line "lp". Fix all of the links that might point at it (they are
  53.  * moved to offset 0 of the next line. Unlink the line from whatever buffer it
  54.  * might be in. Release the memory. The buffers are updated too; the magic
  55.  * conditions described in the above comments don't hold here.
  56.  */
  57. lfree(lp)
  58. register LINE   *lp;
  59. {
  60.         register BUFFER *bp;
  61.         register WINDOW *wp;
  62.  
  63.         wp = wheadp;
  64.         while (wp != NULL) {
  65.                 if (wp->w_linep == lp)
  66.                         wp->w_linep = lp->l_fp;
  67.                 if (wp->w_dotp  == lp) {
  68.                         wp->w_dotp  = lp->l_fp;
  69.                         wp->w_doto  = 0;
  70.                 }
  71.                 if (wp->w_markp == lp) {
  72.                         wp->w_markp = lp->l_fp;
  73.                         wp->w_marko = 0;
  74.                 }
  75.                 wp = wp->w_wndp;
  76.         }
  77.         bp = bheadp;
  78.         while (bp != NULL) {
  79.                 if (bp->b_nwnd == 0) {
  80.                         if (bp->b_dotp  == lp) {
  81.                                 bp->b_dotp = lp->l_fp;
  82.                                 bp->b_doto = 0;
  83.                         }
  84.                         if (bp->b_markp == lp) {
  85.                                 bp->b_markp = lp->l_fp;
  86.                                 bp->b_marko = 0;
  87.                         }
  88.                 }
  89.                 bp = bp->b_bufp;
  90.         }
  91.         lp->l_bp->l_fp = lp->l_fp;
  92.         lp->l_fp->l_bp = lp->l_bp;
  93.         free((char *) lp);
  94. }
  95.  
  96. /*
  97.  * This routine gets called when a character is changed in place in the current
  98.  * buffer. It updates all of the required flags in the buffer and window
  99.  * system. The flag used is passed as an argument; if the buffer is being
  100.  * displayed in more than 1 window we change EDIT t HARD. Set MODE if the
  101.  * mode line needs to be updated (the "*" has to be set).
  102.  */
  103. lchange(flag)
  104. register int    flag;
  105. {
  106.         register WINDOW *wp;
  107.  
  108.         if (curbp->b_nwnd != 1)                 /* Ensure hard.         */
  109.                 flag = WFHARD;
  110.         if ((curbp->b_flag&BFCHG) == 0) {       /* First change, so     */
  111.                 flag |= WFMODE;                 /* update mode lines.   */
  112.                 curbp->b_flag |= BFCHG;
  113.         }
  114.         wp = wheadp;
  115.         while (wp != NULL) {
  116.                 if (wp->w_bufp == curbp)
  117.                         wp->w_flag |= flag;
  118.                 wp = wp->w_wndp;
  119.         }
  120. }
  121.  
  122. /*
  123.  * Insert "n" copies of the character "c" at the current location of dot. In
  124.  * the easy case all that happens is the text is stored in the line. In the
  125.  * hard case, the line has to be reallocated. When the window list is updated,
  126.  * take special care; I screwed it up once. You always update dot in the
  127.  * current window. You update mark, and a dot in another window, if it is
  128.  * greater than the place where you did the insert. Return TRUE if all is
  129.  * well, and FALSE on errors.
  130.  */
  131. linsert(n, c)
  132. {
  133.         register char   *cp1;
  134.         register char   *cp2;
  135.         register LINE   *lp1;
  136.         register LINE   *lp2;
  137.         register LINE   *lp3;
  138.         register int    doto;
  139.         register int    i;
  140.         register WINDOW *wp;
  141.  
  142.         lchange(WFEDIT);
  143.         lp1 = curwp->w_dotp;                    /* Current line         */
  144.         if (lp1 == curbp->b_linep) {            /* At the end: special  */
  145.                 if (curwp->w_doto != 0) {
  146.                         mlwrite("bug: linsert");
  147.                         return (FALSE);
  148.                 }
  149.                 if ((lp2=lalloc(n)) == NULL)    /* Allocate new line    */
  150.                         return (FALSE);
  151.                 lp3 = lp1->l_bp;                /* Previous line        */
  152.                 lp3->l_fp = lp2;                /* Link in              */
  153.                 lp2->l_fp = lp1;
  154.                 lp1->l_bp = lp2;
  155.                 lp2->l_bp = lp3;
  156.                 for (i=0; i<n; ++i)
  157.                         lp2->l_text[i] = c;
  158.                 curwp->w_dotp = lp2;
  159.                 curwp->w_doto = n;
  160.                 return (TRUE);
  161.         }
  162.         doto = curwp->w_doto;                   /* Save for later.      */
  163.         if (lp1->l_used+n > lp1->l_size) {      /* Hard: reallocate     */
  164.                 if ((lp2=lalloc(lp1->l_used+n)) == NULL)
  165.                         return (FALSE);
  166.                 cp1 = &lp1->l_text[0];
  167.                 cp2 = &lp2->l_text[0];
  168.                 while (cp1 != &lp1->l_text[doto])
  169.                         *cp2++ = *cp1++;
  170.                 cp2 += n;
  171.                 while (cp1 != &lp1->l_text[lp1->l_used])
  172.                         *cp2++ = *cp1++;
  173.                 lp1->l_bp->l_fp = lp2;
  174.                 lp2->l_fp = lp1->l_fp;
  175.                 lp1->l_fp->l_bp = lp2;
  176.                 lp2->l_bp = lp1->l_bp;
  177.                 free((char *) lp1);
  178.         } else {                                /* Easy: in place       */
  179.                 lp2 = lp1;                      /* Pretend new line     */
  180.                 lp2->l_used += n;
  181.                 cp2 = &lp1->l_text[lp1->l_used];
  182.                 cp1 = cp2-n;
  183.                 while (cp1 != &lp1->l_text[doto])
  184.                         *--cp2 = *--cp1;
  185.         }
  186.         for (i=0; i<n; ++i)                     /* Add the characters   */
  187.                 lp2->l_text[doto+i] = c;
  188.         wp = wheadp;                            /* Update windows       */
  189.         while (wp != NULL) {
  190.                 if (wp->w_linep == lp1)
  191.                         wp->w_linep = lp2;
  192.                 if (wp->w_dotp == lp1) {
  193.                         wp->w_dotp = lp2;
  194.                         if (wp==curwp || wp->w_doto>doto)
  195.                                 wp->w_doto += n;
  196.                 }
  197.                 if (wp->w_markp == lp1) {
  198.                         wp->w_markp = lp2;
  199.                         if (wp->w_marko > doto)
  200.                                 wp->w_marko += n;
  201.                 }
  202.                 wp = wp->w_wndp;
  203.         }
  204.         return (TRUE);
  205. }
  206.  
  207. /*
  208.  * Insert a newline into the buffer at the current location of dot in the
  209.  * current window. The funny ass-backwards way it does things is not a botch;
  210.  * it just makes the last line in the file not a special case. Return TRUE if
  211.  * everything works out and FALSE on error (memory allocation failure). The
  212.  * update of dot and mark is a bit easier then in the above case, because the
  213.  * split forces more updating.
  214.  */
  215. lnewline()
  216. {
  217.         register char   *cp1;
  218.         register char   *cp2;
  219.         register LINE   *lp1;
  220.         register LINE   *lp2;
  221.         register int    doto;
  222.         register WINDOW *wp;
  223.  
  224.         lchange(WFHARD);
  225.         lp1  = curwp->w_dotp;                   /* Get the address and  */
  226.         doto = curwp->w_doto;                   /* offset of "."        */
  227.         if ((lp2=lalloc(doto)) == NULL)         /* New first half line  */
  228.                 return (FALSE);
  229.         cp1 = &lp1->l_text[0];                  /* Shuffle text around  */
  230.         cp2 = &lp2->l_text[0];
  231.         while (cp1 != &lp1->l_text[doto])
  232.                 *cp2++ = *cp1++;
  233.         cp2 = &lp1->l_text[0];
  234.         while (cp1 != &lp1->l_text[lp1->l_used])
  235.                 *cp2++ = *cp1++;
  236.         lp1->l_used -= doto;
  237.         lp2->l_bp = lp1->l_bp;
  238.         lp1->l_bp = lp2;
  239.         lp2->l_bp->l_fp = lp2;
  240.         lp2->l_fp = lp1;
  241.         wp = wheadp;                            /* Windows              */
  242.         while (wp != NULL) {
  243.                 if (wp->w_linep == lp1)
  244.                         wp->w_linep = lp2;
  245.                 if (wp->w_dotp == lp1) {
  246.                         if (wp->w_doto < doto)
  247.                                 wp->w_dotp = lp2;
  248.                         else
  249.                                 wp->w_doto -= doto;
  250.                 }
  251.                 if (wp->w_markp == lp1) {
  252.                         if (wp->w_marko < doto)
  253.                                 wp->w_markp = lp2;
  254.                         else
  255.                                 wp->w_marko -= doto;
  256.                 }
  257.                 wp = wp->w_wndp;
  258.         }       
  259.         return (TRUE);
  260. }
  261.  
  262. /*
  263.  * This function deletes "n" bytes, starting at dot. It understands how do deal
  264.  * with end of lines, etc. It returns TRUE if all of the characters were
  265.  * deleted, and FALSE if they were not (because dot ran into the end of the
  266.  * buffer. The "kflag" is TRUE if the text should be put in the kill buffer.
  267.  */
  268. ldelete(n, kflag)
  269. {
  270.         register char   *cp1;
  271.         register char   *cp2;
  272.         register LINE   *dotp;
  273.         register int    doto;
  274.         register int    chunk;
  275.         register WINDOW *wp;
  276.  
  277.         while (n != 0) {
  278.                 dotp = curwp->w_dotp;
  279.                 doto = curwp->w_doto;
  280.                 if (dotp == curbp->b_linep)     /* Hit end of buffer.   */
  281.                         return (FALSE);
  282.                 chunk = dotp->l_used-doto;      /* Size of chunk.       */
  283.                 if (chunk > n)
  284.                         chunk = n;
  285.                 if (chunk == 0) {               /* End of line, merge.  */
  286.                         lchange(WFHARD);
  287.                         if (ldelnewline() == FALSE
  288.                         || (kflag!=FALSE && kinsert('\n')==FALSE))
  289.                                 return (FALSE);
  290.                         --n;
  291.                         continue;
  292.                 }
  293.                 lchange(WFEDIT);
  294.                 cp1 = &dotp->l_text[doto];      /* Scrunch text.        */
  295.                 cp2 = cp1 + chunk;
  296.                 if (kflag != FALSE) {           /* Kill?                */
  297.                         while (cp1 != cp2) {
  298.                                 if (kinsert(*cp1) == FALSE)
  299.                                         return (FALSE);
  300.                                 ++cp1;
  301.                         }
  302.                         cp1 = &dotp->l_text[doto];
  303.                 }
  304.                 while (cp2 != &dotp->l_text[dotp->l_used])
  305.                         *cp1++ = *cp2++;
  306.                 dotp->l_used -= chunk;
  307.                 wp = wheadp;                    /* Fix windows          */
  308.                 while (wp != NULL) {
  309.                         if (wp->w_dotp==dotp && wp->w_doto>=doto) {
  310.                                 wp->w_doto -= chunk;
  311.                                 if (wp->w_doto < doto)
  312.                                         wp->w_doto = doto;
  313.                         }       
  314.                         if (wp->w_markp==dotp && wp->w_marko>=doto) {
  315.                                 wp->w_marko -= chunk;
  316.                                 if (wp->w_marko < doto)
  317.                                         wp->w_marko = doto;
  318.                         }
  319.                         wp = wp->w_wndp;
  320.                 }
  321.                 n -= chunk;
  322.         }
  323.         return (TRUE);
  324. }
  325.  
  326. /*
  327.  * Delete a newline. Join the current line with the next line. If the next line
  328.  * is the magic header line always return TRUE; merging the last line with the
  329.  * header line can be thought of as always being a successful operation, even
  330.  * if nothing is done, and this makes the kill buffer work "right". Easy cases
  331.  * can be done by shuffling data around. Hard cases require that lines be moved
  332.  * about in memory. Return FALSE on error and TRUE if all looks ok. Called by
  333.  * "ldelete" only.
  334.  */
  335. ldelnewline()
  336. {
  337.         register char   *cp1;
  338.         register char   *cp2;
  339.         register LINE   *lp1;
  340.         register LINE   *lp2;
  341.         register LINE   *lp3;
  342.         register WINDOW *wp;
  343.  
  344.         lp1 = curwp->w_dotp;
  345.         lp2 = lp1->l_fp;
  346.         if (lp2 == curbp->b_linep) {            /* At the buffer end.   */
  347.                 if (lp1->l_used == 0)           /* Blank line.          */
  348.                         lfree(lp1);
  349.                 return (TRUE);
  350.         }
  351.         if (lp2->l_used <= lp1->l_size-lp1->l_used) {
  352.                 cp1 = &lp1->l_text[lp1->l_used];
  353.                 cp2 = &lp2->l_text[0];
  354.                 while (cp2 != &lp2->l_text[lp2->l_used])
  355.                         *cp1++ = *cp2++;
  356.                 wp = wheadp;
  357.                 while (wp != NULL) {
  358.                         if (wp->w_linep == lp2)
  359.                                 wp->w_linep = lp1;
  360.                         if (wp->w_dotp == lp2) {
  361.                                 wp->w_dotp  = lp1;
  362.                                 wp->w_doto += lp1->l_used;
  363.                         }
  364.                         if (wp->w_markp == lp2) {
  365.                                 wp->w_markp  = lp1;
  366.                                 wp->w_marko += lp1->l_used;
  367.                         }
  368.                         wp = wp->w_wndp;
  369.                 }               
  370.                 lp1->l_used += lp2->l_used;
  371.                 lp1->l_fp = lp2->l_fp;
  372.                 lp2->l_fp->l_bp = lp1;
  373.                 free((char *) lp2);
  374.                 return (TRUE);
  375.         }
  376.         if ((lp3=lalloc(lp1->l_used+lp2->l_used)) == NULL)
  377.                 return (FALSE);
  378.         cp1 = &lp1->l_text[0];
  379.         cp2 = &lp3->l_text[0];
  380.         while (cp1 != &lp1->l_text[lp1->l_used])
  381.                 *cp2++ = *cp1++;
  382.         cp1 = &lp2->l_text[0];
  383.         while (cp1 != &lp2->l_text[lp2->l_used])
  384.                 *cp2++ = *cp1++;
  385.         lp1->l_bp->l_fp = lp3;
  386.         lp3->l_fp = lp2->l_fp;
  387.         lp2->l_fp->l_bp = lp3;
  388.         lp3->l_bp = lp1->l_bp;
  389.         wp = wheadp;
  390.         while (wp != NULL) {
  391.                 if (wp->w_linep==lp1 || wp->w_linep==lp2)
  392.                         wp->w_linep = lp3;
  393.                 if (wp->w_dotp == lp1)
  394.                         wp->w_dotp  = lp3;
  395.                 else if (wp->w_dotp == lp2) {
  396.                         wp->w_dotp  = lp3;
  397.                         wp->w_doto += lp1->l_used;
  398.                 }
  399.                 if (wp->w_markp == lp1)
  400.                         wp->w_markp  = lp3;
  401.                 else if (wp->w_markp == lp2) {
  402.                         wp->w_markp  = lp3;
  403.                         wp->w_marko += lp1->l_used;
  404.                 }
  405.                 wp = wp->w_wndp;
  406.         }
  407.         free((char *) lp1);
  408.         free((char *) lp2);
  409.         return (TRUE);
  410. }
  411.  
  412. /*
  413.  * Delete all of the text saved in the kill buffer. Called by commands when a
  414.  * new kill context is being created. The kill buffer array is released, just
  415.  * in case the buffer has grown to immense size. No errors.
  416.  */
  417. kdelete()
  418. {
  419.         if (kbufp != NULL) {
  420.                 free((char *) kbufp);
  421.                 kbufp = NULL;
  422.                 kused = 0;
  423.                 ksize = 0;
  424.         }
  425. }
  426.  
  427. /*
  428.  * Insert a character to the kill buffer, enlarging the buffer if there isn't
  429.  * any room. Always grow the buffer in chunks, on the assumption that if you
  430.  * put something in the kill buffer you are going to put more stuff there too
  431.  * later. Return TRUE if all is well, and FALSE on errors.
  432.  */
  433. kinsert(c)
  434. {
  435.         register char   *nbufp;
  436.         register int    i;
  437.  
  438.         if (kused == ksize) {
  439.                 if ((nbufp=malloc(ksize+KBLOCK)) == NULL)
  440.                         return (FALSE);
  441.                 for (i=0; i<ksize; ++i)
  442.                         nbufp[i] = kbufp[i];
  443.                 if (kbufp != NULL)
  444.                         free((char *) kbufp);
  445.                 kbufp  = nbufp;
  446.                 ksize += KBLOCK;
  447.         }
  448.         kbufp[kused++] = c;
  449.         return (TRUE);
  450. }
  451.  
  452. /*
  453.  * This function gets characters from the kill buffer. If the character index
  454.  * "n" is off the end, it returns "-1". This lets the caller just scan along
  455.  * until it gets a "-1" back.
  456.  */
  457. kremove(n)
  458. {
  459.         if (n >= kused)
  460.                 return (-1);
  461.         else
  462.                 return (kbufp[n] & 0xFF);
  463. }
  464.  
  465.  
  466.